<?php

namespace App\Services\YoudaoZhiyun;

class TranslateServices extends BaseService
{
    /**
     *文本翻译
     * @param $q 文本内容
     *author:tangfen
     *date:$ct en zh-CHS
     */
    public function textTranslate($q,$to)
    {
        if (empty($q)|| empty($to)) {
            return false;
        }
        $salt = $this->create_guid();
        $args = array(
            'q' => $q,
            'appKey' => $this->TEXT_APP_KEY,
            'salt' => $salt,
        );
        $args['from'] = 'auto';//源语言 （自动识别）
        $args['to'] = $to;//目标语言
        $args['signType'] = 'v3';
        $curtime = strtotime("now");
        $args['curtime'] = $curtime;
        $signStr = $this->TEXT_APP_KEY . $this->truncate($q) . $salt . $curtime . $this->TEXT_SEC_KEY;
        $args['sign'] = hash("sha256", $signStr);
//        $args['vocabId'] = '94D590C55F9F411788F994CCF0B2979E';//您的用户词表ID(术语表)非必填
        $ret = $this->call($this->TEXT_URL, $args);
        return json_decode($ret, true);
    }

    /**
     *图片翻译
     * @param $q  type为1时必填,图片的Base64编码
     *author:tangfen
     *date:$ct
     */
    public function imgTranslate($q,$to)
    {
        if (empty($q)||empty($to)) {
            return false;
        }
        $salt = $this->create_guid();
        $args = array(
            'appKey' => $this->IMG_APP_KEY,
            'salt' => $salt,
        );
        $type = '1';
        $args['from'] = 'auto';  //源语言（自动识别）
        $args['to'] = $to;     //目标语言
        $args['type'] = $type;
        $args['q'] = $q;
        $signStr = $this->IMG_APP_KEY . $q . $salt . $this->IMG_SEC_KEY;
        $args['sign'] = md5($signStr);
        $ret = $this->call($this->IMG_URL, $args);
        $ret = json_decode($ret, true);
        return $ret;
    }


    /**
     *翻译语音
     * @param $q
     * @param $to
     *author:tangfen
     *date:$ct
     */
    public function voiceTranslate($q,$to)
    {
        if (empty($q)||empty($to)) {
            return false;
        }
        $salt = $this->create_guid();
        $args = array(
            'q' => $q,
            'appKey' => $this->VOICE_APP_KEY,
            'salt' => $salt,
            'channel' => 1,//声道数， 仅支持单声道，请填写固定值1
            'rate' => '16000',//采样率，推荐 16000 采用率
            'format' => 'wav',//语音文件的格式， 目前只支持wav，不区分大小写
            'type' => "1",
            'signType' => "v1",
        );
        $args['from'] = 'auto';
        $args['to'] = $to;
        $signStr = $this->VOICE_APP_KEY . $q . $salt . $this->VOICE_SEC_KEY;
        $args['sign'] = md5($signStr);
        $ret = $this->call($this->VOICE_URL, $args);
        $ret = json_decode($ret, true);
        return $ret;
    }

}